package com.kagura.ws.service.impl;

import com.kagura.ws.config.SoapConst;
import com.kagura.ws.service.ScheduleService;
import org.dom4j.DocumentHelper;
import org.w3c.dom.Document;
import javax.jws.WebService;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

/**
 * @PACKAGE_NAME: com.kagura.ws.service.impl
 * @CLASS_NAME: SchedultServiceImpl
 * @USER: kagura
 * @Description TODO 接口实现类
 * @Date 2021/5/23 3:03 下午
 * @MONTH_NAME_FULL: 五月
 * @DAY: 23
 * @DAY_NAME_FULL: 星期日
 * @PROJECT_NAME: ws
 **/
@WebService(
        targetNamespace = SoapConst.NAMESPACE_URI, // wsdl命名空间
        name = "claSoap", // portType名称，客户端生成代码时，为接口名称
        serviceName = "claSoapSoapService", // 服务name名称
        portName = "claSoapPortName", // port名称
        endpointInterface = "com.kagura.ws.service.ScheduleService"
)
public class SchedultServiceImpl implements ScheduleService {

    // 创建解析器工厂
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

    @Override
    public String getUserInfo(String pInput) {
        // 解析xml，使用xpath路径进行取值
        try {
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            StringBuffer stringBuffer = new StringBuffer(pInput);
            // 把xml文件加载为流，供Document加载
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringBuffer.toString().getBytes(StandardCharsets.UTF_8));
            // 解析v3格式的xml需要使用w3c的包，采用dom解析
            Document document = documentBuilder.parse(byteArrayInputStream);
            document.setXmlStandalone(true);

            // 创建Xpath对象，使用Xpath路径获取入参中指定值
            XPath xPath = XPathFactory.newInstance().newXPath();

            // 创建生成xml的Document对象，考虑效率，生成xml使用dom4j的Document对象来生成，不使用w3c的包
            org.dom4j.Document createXmlDocument = DocumentHelper.createDocument();

            // 判断事件编码
            String codePath = "/PRPA_IN201311UV02/interactionId/@extension";
            String codeValue = (String) xPath.evaluate(codePath, document, XPathConstants.STRING);
            if (codeValue.equals("JH0001")) {
                // 创建跟节点
                org.dom4j.Element rootElement = createXmlDocument.addElement("RESPONSE");

                // 创建二级节点(患者id)
                org.dom4j.Element patientId = rootElement.addElement("PATIENT_ID");
                // 获取患者id
                String patientIdPath = "/PRPA_IN201311UV02/controlActProcess/subject/registrationRequest/subject1/patient/id/item/@extension";
                String patientIdValue = (String) xPath.evaluate(patientIdPath, document, XPathConstants.STRING);
                // 节点赋值
                patientId.setText(patientIdValue);

                // 患者姓名
                org.dom4j.Element patientName = rootElement.addElement("PATIENT_NAME");
                String patientNamePath = "/PRPA_IN201311UV02/controlActProcess/subject/registrationRequest/subject1/patient/patientPerson/name/item/part/@value";
                String patientNameValue = (String) xPath.evaluate(patientNamePath, document, XPathConstants.STRING);
                patientName.setText(patientNameValue);

                // 患者出生日期
                org.dom4j.Element birthDate = rootElement.addElement("BIRTH_DATE");
                String birthDatePath = "/PRPA_IN201311UV02/controlActProcess/subject/registrationRequest/subject1/patient/patientPerson/birthTime/@value";
                String birthDateValue = (String) xPath.evaluate(birthDatePath, document, XPathConstants.STRING);
                birthDate.setText(birthDateValue);

                // 患者现住址
                org.dom4j.Element address = rootElement.addElement("ADDRESS");
                String addressPath = "/PRPA_IN201311UV02/controlActProcess/subject/registrationRequest/subject1/patient/patientPerson/addr/item/part[1]/@value";
                String addressValue = (String) xPath.evaluate(addressPath, document, XPathConstants.STRING);
                address.setText(addressValue);

                // 患者民族代码
                org.dom4j.Element nationCode = rootElement.addElement("NATION_CODE");
                String nationCodePath = "/PRPA_IN201311UV02/controlActProcess/subject/registrationRequest/subject1/patient/patientPerson/ethnicGroupCode/item/@code";
                String nationCodeValue = (String) xPath.evaluate(nationCodePath, document, XPathConstants.STRING);
                nationCode.setText(nationCodeValue);

                // 患者民族名称
                org.dom4j.Element nationName = rootElement.addElement("NATION_NAME");
                String nationNamePath = "/PRPA_IN201311UV02/controlActProcess/subject/registrationRequest/subject1/patient/patientPerson/ethnicGroupCode/item/displayName/@value";
                String nationNameValue = (String) xPath.evaluate(nationNamePath, document, XPathConstants.STRING);
                nationName.setText(nationNameValue);

                return createXmlDocument.asXML().toString();

            } else {
                // 创建事件编码不对处理
                org.dom4j.Element codeParseFailed = createXmlDocument.addElement("RESPONSE");
                // 创建二级节点
                org.dom4j.Element resultCode = codeParseFailed.addElement("RESULT_CODE");
                resultCode.setText("false");

                org.dom4j.Element resultName = codeParseFailed.addElement("RESULT_NAME");
                resultName.setText("事件编码不为JH0001，请检查入参");

                return createXmlDocument.asXML().toString();
            }
        } catch (Exception e) {
            org.dom4j.Document document_4j = DocumentHelper.createDocument();
            // 创建解析失败xml对象
            org.dom4j.Element parseFailed = document_4j.addElement("RESPONSE");
            // 创建二级节点
            org.dom4j.Element resultCode = parseFailed.addElement("RESULT_CODE");
            resultCode.setText("false");
            org.dom4j.Element resultName = parseFailed.addElement("RESULT_NAME");
            resultName.setText("xml解析失败, 请检查入参格式");

            return document_4j.asXML().toString();
        }
    }
}
